<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <header th:replace="header::html"></header>
</head>
<body>
<form class="layui-form" action="">
    <div class="mainBox">
        <div class="main-container">
            <div class="main-container">
                <input type="text" id="menuId" th:value="${myMenu.menuId}" name="menuId" style="display:none;" autocomplete="off" class="layui-input">
                <div class="layui-form-item">
                    <label class="layui-form-label">
                        <span style="color: red">*</span>菜单名
                    </label>
                    <div class="layui-input-block">
                        <input type="text" th:value="${myMenu.menuName}" name="menuName" lay-verify="menuName" autocomplete="off" placeholder="请输入菜单名" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">图标</label>
                    <div class="layui-input-block">
                        <input type="text" id="iconPicker" name="icon" class="hide" th:value="${myMenu.icon}">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">路径</label>
                    <div class="layui-input-block">
                        <input type="text"  name="url" th:value="${myMenu.url}" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">权限标识</label>
                    <div class="layui-input-block">
                        <input type="text"  name="permission" th:value="${myMenu.permission}" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">
                        <span style="color: red">*</span>排序
                    </label>
                    <div class="layui-input-block">
                        <input type="text"  name="sort" th:value="${myMenu.sort}" lay-verify="sort" autocomplete="off" placeholder="请输入排序值" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">
                        <span style="color: red">*</span>类型
                    </label>
                    <div class="layui-input-block">
                        <input type="radio" name="type" value="0" title="目录"  th:checked="${myMenu.type == 0}? 'true':'false'" lay-verify="radio">
                        <input type="radio" name="type" value="1" title="菜单"  th:checked="${myMenu.type == 1}? 'true':'false'" lay-verify="radio">
                        <input type="radio" name="type" value="2" title="按钮" th:checked="${myMenu.type == 2}? 'true':'false'" lay-verify="radio">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">
                        上级菜单
                    </label>
                    <div class="layui-input-block">
                        <input type="number" id="parentId" th:value="${myMenu.parentId}" name="parentId"  lay-verify="parentId" style="display:none;width: 0px" autocomplete="off" class="layui-input">
                        <ul id="dataTree" class="dtree" data-id="0" th:data-value="${myMenu.parentId}"></ul>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="bottom">
        <div class="button-container">
            <button type="submit" class="layui-btn layui-btn-normal layui-btn-sm" lay-submit="" lay-filter="menu-save">
                <i class="layui-icon layui-icon-ok"></i>
                提交
            </button>
            <button type="reset" class="layui-btn layui-btn-primary layui-btn-sm">
                <i class="layui-icon layui-icon-refresh"></i>
                重置
            </button>
        </div>
    </div>
</form>
<script type="text/javascript">

    layui.use(['iconPicker','dtree','form','jquery'],function(){
        let form = layui.form;
        let $ = layui.jquery;
        let dtree = layui.dtree;
        var iconPicker = layui.iconPicker;
        // 初始化树
        dtree.render({
            elem: "#dataTree",
            initLevel: "1",
            width: "100%",
            method: 'get',
            dataStyle: "layuiStyle",  //使用layui风格的数据格式
            response:{message:"msg",statusCode:200},  //修改response中返回数据的定义
            url: "/api/menu/build",
            dataFormat: "list",  //配置data的风格为list
            select: true, //指定下拉树模式
            selectTips: "不选默认是顶级目录",
            selectCardHeight: "200",
        });
        iconPicker.render({
            // 选择器，推荐使用input
            elem: '#iconPicker',
            // 数据类型：fontClass/unicode，推荐使用fontClass
            type: 'fontClass',
            // 是否开启搜索：true/false，默认true
            search: true,
            // 是否开启分页：true/false，默认true
            page: true,
            // 每页显示数量，默认12
            limit: 12,
            // 点击回调
            click: function (data) {
                console.log(data);
            },
            // 渲染成功后的回调
            success: function(d) {
                console.log(d);
            }
        });
        var param = dtree.getNowParam("dataTree");
        formDate = $("#parentId");
        dtree.on("node('dataTree')" ,function(obj){
            var param = dtree.getNowParam("dataTree");
            $("#parentId").val(param.nodeId);
            formDate = $("#parentId");
        });
        form.verify({
            menuName: function(value){
                if(value.length < 2){
                    return '菜单名至少2个字符';
                }
            },
            sort:  [
                /^[1-9]\d*$/
            ,'只能是整数哦'
        ],
            radio: function(value,item){
                var $ = layui.$;
                var verifyName=$(item).attr('name')
                    , verifyType=$(item).attr('type')
                    ,formElem=$(item).parents('.layui-form')//获取当前所在的form元素，如果存在的话
                    ,verifyElem=formElem.find('input[name='+verifyName+']')//获取需要校验的元素
                    ,isTrue= verifyElem.is(':checked')//是否命中校验
                    ,focusElem = verifyElem.next().find('i.layui-icon');//焦点元素
                if(!isTrue || !value){
                    //定位焦点
                    focusElem.css(verifyType=='radio'?{"color":"#FF5722"}:{"border-color":"#FF5722"});
                    //对非输入框设置焦点
                    focusElem.first().attr("tabIndex","1").css("outline","0").blur(function() {
                        focusElem.css(verifyType=='radio'?{"color":""}:{"border-color":""});
                    }).focus();
                    return '必填项不能为空';
                }
            }
    });
        form.on('submit(menu-save)', function(data){
            $.ajax({
                url:'/api/menu',
                data:JSON.stringify(data.field),
                dataType:'json',
                contentType:'application/json',
                type:'put',
                success:function(result){
                    if(result.success){
                        layer.msg(result.msg,{icon:1,time:1000},function(){
                            parent.layer.close(parent.layer.getFrameIndex(window.name));//关闭当前页
                            parent.location.reload();//刷新页面
                        });
                    }else{
                        layer.msg(result.msg,{icon:2,time:1000});
                    }
                }
            })
            return false;
        });
    })
</script>
<script type="text/javascript">
</script>
</body>
</html>
